//
//  ArmListViewCell.m
//  wAudioShare
//
//  Created by Anson on 14-11-8.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "ArmListViewCell.h"
#import "ArmModel.h"
#import "AutomationCell.h"

@interface ArmListViewCell ()

@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UIButton *btnEdit;
@property (weak, nonatomic) IBOutlet UISwitch *swLink;

@end

@implementation ArmListViewCell

- (void)awakeFromNib {
    // Initialization code
    UIImage *img1  = [UIImage imageNamed:@"bg_cell"];
    UIImage *img2  = [UIImage imageNamed:@"bg_cell_sel"];
    
    UIImageView *cellbg1 = [[UIImageView alloc]initWithImage:img1];
    UIImageView *cellbg2 = [[UIImageView alloc]initWithImage:img2];
    
    self.backgroundView = cellbg1;
    self.selectedBackgroundView = cellbg2;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setArmData:(ArmModel *)armData {
    
    self.labelTitle.text = armData.label;
    [self.swLink setOn:armData.active];
    _armData = armData;
    
    [self updateImgIcon];
}

- (IBAction)onSwitchValChange:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(armListViewCellSwitch:valueChanged:withArmData:)]) {
        [self.delegate armListViewCellSwitch:sender valueChanged:self.swLink.isOn withArmData:self.armData];
    }
}

- (IBAction)editBtnClicked:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(armListViewCellEditBtnWithArmData:)]) {
        [self.delegate armListViewCellEditBtnWithArmData:self.armData];
    }
}

- (void)updateImgIcon {
    
    NSInteger deviceCMDId = [self.armData.command integerValue];
    switch (deviceCMDId) {
        case 37://switch or Meter
            self.icon.image = [UIImage imageNamed:@"icon_socket_close"];
            break;
        case 48://BinarySensor
            self.icon.image = [UIImage imageNamed:@"icon_sensor_open"];
            break;
        case 49://MultilevelSensor or Thermostat
            self.icon.image = [UIImage imageNamed:@"icon_infrared"];
            break;
        case 38://Dimmer or Motor
            self.icon.image = [UIImage imageNamed:@"icon_curtain_close"];
            break;
        case 98://DoorLock
            self.icon.image = [UIImage imageNamed:@"icon_doorlock_close"];
            break;
        default:
            break;
    }

}

-(IBAction)changeInfo:(id)sender
{
    //[self buttonSelectedWithSound];
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeInfo:cell:)])
    {
        [self.delegate changeInfo:[self.armData jsonObj] cell:self];
    }
}

@end
